package me.landery.modules.sinco.rest;

import me.landery.annotation.Log;
import me.landery.exception.BadRequestException;
import me.landery.modules.sinco.domain.Application;
import me.landery.modules.sinco.service.ApplicationService;
import me.landery.modules.sinco.service.dto.ApplicationDto;
import me.landery.modules.sinco.service.dto.ApplicationQueryCriteria;
import me.landery.utils.SecurityUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @website https://el-admin.vip
 * @author landery
 * @date 2022-06-17
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "应用管理")
@RequestMapping("/sinco/app")
public class ApplicationController {

    private final ApplicationService appService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('app:list','admin')")
    public void exportApp(HttpServletResponse response, ApplicationQueryCriteria criteria) throws IOException {
        appService.download(appService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询应用")
    @ApiOperation("查询应用")
    @PreAuthorize("@el.check('app:list','admin')")
    public ResponseEntity<Object> queryApp(ApplicationQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(appService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/web")
//    @Log("查询应用")
    @ApiOperation("查询应用")
    @PreAuthorize("@el.check('app:weblist','admin')")
    public ResponseEntity<Object> queryAppWeb(ApplicationQueryCriteria criteria, Pageable pageable){
        if (criteria.getUsername() != null && !criteria.getUsername().equals(SecurityUtils.getCurrentUsername())) {
            throw new BadRequestException("查询失败，只能查询自己的应用");
        }
        Pageable pageable1 = PageRequest.of(pageable.getPageNumber()-1,pageable.getPageSize(),pageable.getSort());
        return new ResponseEntity<>(appService.queryAll(criteria,pageable1),HttpStatus.OK);
    }

//    @GetMapping("/normal/web")
////    @Log("查询应用")
//    @ApiOperation("查询未禁用应用")
////    @PreAuthorize("@el.check('app:list')")
//    public ResponseEntity<Object> queryNormalAppWeb(ApplicationQueryCriteria criteria, Pageable pageable){
//        Pageable pageable1 = PageRequest.of(pageable.getPageNumber()-1,pageable.getPageSize(),pageable.getSort());
//        return new ResponseEntity<>(appService.queryAll(criteria,pageable1),HttpStatus.OK);
//    }

    @PostMapping
    @Log("新增应用")
    @ApiOperation("新增应用")
    @PreAuthorize("@el.check('app:add','admin')")
    public ResponseEntity<Object> createApp(@Validated @RequestBody Application resources){
        return new ResponseEntity<>(appService.create(resources),HttpStatus.CREATED);
    }

    @PostMapping("/web")
    @Log("新增应用")
    @ApiOperation("新增应用")
    @PreAuthorize("@el.check('app:webadd','admin')")
    public ResponseEntity<Object> createAppWeb(@Validated @RequestBody ApplicationDto applicationDto){
        if (applicationDto.getName() == null){
            throw new BadRequestException("应用名称不能为空");
        }
        return new ResponseEntity<>(appService.create(applicationDto),HttpStatus.CREATED);
    }


    @PutMapping
    @Log("修改应用")
    @ApiOperation("修改应用")
    @PreAuthorize("@el.check('app:edit','admin')")
    public ResponseEntity<Object> updateApp(@Validated @RequestBody Application resources){
        appService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PostMapping("/update/web")
    @Log("修改应用")
    @ApiOperation("修改应用")
    @PreAuthorize("@el.check('app:webedit','admin')")
    public ResponseEntity<Object> updateAppWeb(@Validated @RequestBody ApplicationDto resources){
        appService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除应用")
    @ApiOperation("删除应用")
    @PreAuthorize("@el.check('app:del','admin')")
    public ResponseEntity<Object> deleteApp(@RequestBody Long[] ids) {
        appService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/delete/web")
    @Log("删除应用")
    @ApiOperation("删除应用")
    @PreAuthorize("@el.check('app:webdel','admin')")
    public ResponseEntity<Object> deleteAppWeb(@RequestBody Long[] ids) {
        appService.deleteAll(ids);
        return new ResponseEntity<>("删除成功",HttpStatus.OK);
    }




}